package com.lavender.async._common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * <p>
 * 接口统一的返回格式
 * 参考：https://www.huaweicloud.com/articles/3c28775162d7bcb1f5558891f8f139e6.html
 * </p>
 *
 * @author: zhu.chen
 * @date: 2020/12/29
 * @version: v1.0.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private String code;

    private String message;

    private T data;

    /**
     * <p>
     * 实例化成功响应码的ApiResponse对象实例
     * </p>
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> instanceSuccessApiResponse() {
        ApiResponse apiResponse = new ApiResponse<T>();
        apiResponse.setData("success");
        apiResponse.setCode(CommonErrorCodeEnum.SUCCESS.getCode());
        apiResponse.setMessage(CommonErrorCodeEnum.SUCCESS.getMessage());
        return apiResponse;
    }

    /**
     * <p>
     * 实例化成功响应码的ApiResponse对象实例
     * </p>
     *
     * @param data 业务data数据
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> instanceSuccessApiResponse(T data) {
        ApiResponse apiResponse = new ApiResponse<T>();
        apiResponse.setData(data);
        apiResponse.setCode(CommonErrorCodeEnum.SUCCESS.getCode());
        apiResponse.setMessage(CommonErrorCodeEnum.SUCCESS.getMessage());
        return apiResponse;
    }

    /**
     * <p>
     * 实例化失败响应码的ApiResponse对象实例
     * </p>
     *
     * @param data 业务data数据
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> instanceFailApiResponse(T data) {
        ApiResponse apiResponse = new ApiResponse<T>();
        apiResponse.setData(data);
        apiResponse.setCode(CommonErrorCodeEnum.FAIL.getCode());
        apiResponse.setMessage(CommonErrorCodeEnum.FAIL.getMessage());
        return apiResponse;
    }

    /**
     * <p>
     * 获取ApiResponse实例
     * </p>
     *
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> instanceApiResponse(IErrorCode errorCode) {
        return instanceApiResponse(null, errorCode, errorCode.getMessage());
    }

    /**
     * <p>
     * 获取ApiResponse实例
     * </p>
     *
     * @param data
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> instanceApiResponse(T data, IErrorCode errorCode) {
        return instanceApiResponse(data, errorCode, errorCode.getMessage());
    }

    /**
     * <p>
     * 获取ApiResponse实例
     * </p>
     *
     * @param data
     * @param errorCode
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> instanceApiResponse(T data, IErrorCode errorCode, String message) {
        ApiResponse apiResponse = new ApiResponse<T>();
        apiResponse.setData(data);
        apiResponse.setCode(errorCode.getCode());
        if (!StringUtils.hasLength(message)) {
            message = errorCode.getMessage();
        }
        apiResponse.setMessage(message);
        return apiResponse;
    }

}
